//
//  RequestHttpNetWrok.m
//  CFFinanceProject
//
//  Created by chunfeng on 2018/4/9.
//  Copyright © 2018年 xiaowen. All rights reserved.
//


#import "RequestHttpNetWrok.h"

@implementation RequestHttpNetWrok

//解决请求一大串字符串URL报错的方法
//NSString *str1 = [MovieRecommendAddress stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];


static RequestHttpNetWrok * operationManager;

+(RequestHttpNetWrok *)shareInstance{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        /**
         服务器地址
         */
        operationManager = [[RequestHttpNetWrok alloc] initWithBaseURL:[NSURL URLWithString:DEF_INTERFACE_WEBVIEWSERVICE]];
        operationManager.requestSerializer.timeoutInterval = 15;
        
        operationManager.requestSerializer = [AFHTTPRequestSerializer serializer];
        operationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        operationManager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", @"text/plain", nil];
    });
    
    return operationManager;
}


/**
 *  post请求
 */
+ (RequestHttpNetWrok *)requestPostWithParameters:(NSDictionary *)parameters
                                        urlString:(NSString *)urlString
                                     finishHandle:(successBlock)finishHandle
                                       failHandle:(failtureBlock)failHandle
{
    RequestHttpNetWrok *manager = [RequestHttpNetWrok shareInstance];
    
//        NSLog(@"parameters %@manager.baseURL  %@%@", parameters,manager.baseURL,urlString);
    
    [manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [RequestHttpNetWrok requestSuccess:responseObject finishHandle:finishHandle failHandle:(failtureBlock)failHandle];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [RequestHttpNetWrok requestError:error failHandle:failHandle];
    }];
    
    return manager;
}


/**
 *  get请求
 */
+ (RequestHttpNetWrok *)requestGettWithParameters:(NSDictionary *)parameters
                                        urlString:(NSString *)urlString
                                     finishHandle:(successBlock)finishHandle
                                       failHandle:(failtureBlock)failHandle
{
    RequestHttpNetWrok *manager = [RequestHttpNetWrok shareInstance];
    
    
    //    NSLog(@"parameters %@manager.baseURL  %@%@", parameters,manager.baseURL,urlString);
    
    [manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        [RequestHttpNetWrok requestSuccess:responseObject finishHandle:finishHandle failHandle:(failtureBlock)failHandle];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [RequestHttpNetWrok requestError:error failHandle:failHandle];
    }];
    
    return manager;
}

/**
 *  网络请求成功处理
 */
+ (void)requestSuccess:(id)responseObject finishHandle:(successBlock)finishHandle failHandle:(failtureBlock)failHandle{
    
    id json = [NSJSONSerialization
                          JSONObjectWithData:responseObject
                          options:kNilOptions
                          error:nil];
    
    
//    NSLog(@"--------------json %@",json);
    
//    if ([json[@"code"] integerValue] == 200) {
        //正确逻辑处理
        finishHandle(json);
//    }else{
//        //错误逻辑处理
//        [CFUtility showTips:json[@"data"][@"message"]];
//
//        NSError *error = [NSError errorWithDomain:ERRORDAOMAIN code:[json[@"code"] integerValue] userInfo:responseObject];
//        failHandle(error);
//    }
}


/**
 *  网络请求失败处理
 */
+ (void)requestError:(NSError *)error failHandle:(failtureBlock)failHandle{
    
    NSString *message;
    
    if(error.code == kCFURLErrorNotConnectedToInternet || error.code == kCFURLErrorCannotConnectToHost)
    {
        message = @"网络不可用，请检查网络连接";
    }else if (error.code == kCFURLErrorTimedOut)
    {
        message = @"网络请求超时";
    }else
    {
        message = @"请求失败，请稍后再试";
    }
    
    [CFUtility showTips:message];
    
    CC_LOG_VALUE(error.localizedDescription);
    
    failHandle(error);
}






/**
 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
 
 [manager GET:@"http://wangyi.butterfly.mopaasapp.com/news/api?type=war&page=1&limit=10" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
 
 }
 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
 
 weakSelf.listArrayList = [[NSMutableArray alloc] initWithCapacity:0];
 
 weakSelf.listArrayList = responseObject[@"list"];
 
 
 [weakSelf.tableView reloadData];
 [MXUtility hideMBProgress:weakSelf.view];
 
 
 [weakSelf.tableView.footer endRefreshing];
 [weakSelf.tableView.header endRefreshing];
 
 }
 
 failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
 
 [MXUtility hideMBProgress:weakSelf.view];
 
 [weakSelf.tableView.footer endRefreshing];
 [weakSelf.tableView.header endRefreshing];
 
 }];
 */

@end
